package com.central.apps.handler.contract;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.central.app.service.HandlerService;
import com.central.apps.handler.base.LvBaseHandler;
import com.central.bizcommon.model.flow.FlowNode;
import com.central.bizcommon.model.flow.FlowOpinion;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class ConprocurechangeHandler extends LvBaseHandler {
    @Autowired
    private HandlerService handlerService;

    @Override
    public String beforeExcuteFlow(String app, Long id, FlowNode flowNode, FlowOpinion op) throws Exception {

        if("appval6".equals(flowNode.getId())&& op.isFlag()){
            List<Map<String, Object>> list = handlerService.findBySql("select * from gq_conprocurechange where id = "+id);
            if(CollectionUtils.isNotEmpty(list)){
                String nprotype = MapUtils.getString(list.get(0),"nprotype");
                String noppcompany = MapUtils.getString(list.get(0),"noppcompany");
                String conid = MapUtils.getString(list.get(0),"conid");

                //更新合同台账
                handlerService.updateBySql("update gq_conaccount set protype ='"+nprotype+"',oppcompany='"+noppcompany+"' where conid = '"+conid+"'");
            }
        }

        return super.beforeExcuteFlow(app, id, flowNode, op);
    }
    @Override
    public void afterSave(String app, String objName, Map<String, Object> data, boolean isUpdate) throws Exception {
        if("gq_oppositeunit".equals(objName)){
            long id = MapUtils.getLongValue(data,"pid");
            String noppcompany = setTwo(id);
            handlerService.updateBySql("update gq_conprocurechange set noppcompany ='"+noppcompany+"' where id = "+id);
        }
    }
    @Override
    public void beforDelete(String app, String objName, Long id, boolean isLogic) {
        String sql = "select  pid from gq_oppositeunit where id = " + id;
        List<Map<String, Object>> list = handlerService.findBySql(sql);
        int pid = MapUtils.getIntValue(list.get(0),"pid");
        if(CollectionUtils.isNotEmpty(list)){
            String sql2 = "select  companyname from gq_oppositeunit where pid = " + pid +" and id !="+id;
            List<Map<String, Object>> list2 = handlerService.findBySql(sql2);
            StringBuffer sb = new StringBuffer();
            String companynames = "";
            if (CollectionUtils.isNotEmpty(list2)) {
                for (Map<String, Object> stringObjectMap : list2) {
                    sb.append(",").append(stringObjectMap.get("companyname"));
                }
                companynames = sb.toString().replaceFirst(",", "");
                handlerService.updateBySql("update gq_conprocurechange set noppcompany ='"+companynames+"' where id = "+pid);
            }
        }
    }
    //汇总供方信息
    public String setTwo(long id) {
        //获取合同申请中供方单位子表中的数据，汇总到乙方名称中 关联关系pid=:id
        String sql = "select  companyname from gq_oppositeunit where pid = " + id;
        List<Map<String, Object>> list2 = handlerService.findBySql(sql);
        StringBuffer sb = new StringBuffer();
        String companynames = "";
        if (CollectionUtils.isNotEmpty(list2)) {
            for (Map<String, Object> stringObjectMap : list2) {
                sb.append(",").append(stringObjectMap.get("companyname"));
            }
            companynames = sb.toString().replaceFirst(",", "");
        }
        return companynames;
    }
}
